package com.lu.manage.modular.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lu.manage.config.web.BeetlConfig;
import com.lu.manage.core.beetl.BeetlConfiguration;
import com.lu.manage.core.common.enums.YesOrNotEnum;
import com.lu.manage.core.common.global.SystemCache;
import com.lu.manage.core.common.global.bean.BaseProperties;
import com.lu.manage.core.utils.ReflexUtils;
import com.lu.manage.core.utils.ToolUtil;
import com.lu.manage.modular.system.model.Dept;
import com.lu.manage.modular.system.model.Dict;
import com.lu.manage.modular.system.model.DictType;
import com.lu.manage.modular.system.model.Role;
import com.lu.manage.modular.system.model.enums.DictTypeEnum;
import com.lu.manage.modular.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program LuBoot
 * @description: 系统缓存
 * @author: zhanglu
 * @create: 2019-11-08 13:44:00
 */
@Service
public class SystemCacheServiceImpl implements SystemCacheService {

    @Autowired
    private DeptService deptService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private DictTypeService dictTypeService;

    @Autowired
    private DictService dictService;

    @Autowired
    private BeetlConfig beetlConfig;

    @Override
    public void init() {
        deptCache();
        roleCache();
        dictTypeDoCache(dictTypeService.getBaseMapper().selectList(Wrappers.<DictType>lambdaQuery().in(DictType::getCode, DictTypeEnum.codes()).eq(DictType::getState, YesOrNotEnum.Y.getCode())));
    }

    @Override
    public void deptCache() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("id", "pid", "pids", "simple_name");
        List<Dept> deptList = deptService.getBaseMapper().selectList(queryWrapper);
        SystemCache.deptMap = deptList.stream().collect(Collectors.toMap(Dept::getId, o -> o));
    }

    @Override
    public void roleCache() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("role_id", "pid", "name");
        List<Role> roleList = roleService.getBaseMapper().selectList(queryWrapper);
        SystemCache.roleMap = roleList.stream().collect(Collectors.toMap(Role::getRoleId, o -> o));
    }

    @Override
    public void dictTypeCache(String dictTypeIds) {
        List<String> ids = JSONArray.parseArray(dictTypeIds, String.class);
        if(ToolUtil.isNotEmpty(ids)){
            System.out.println(1);
            dictTypeDoCache(dictTypeService.getBaseMapper().selectList(Wrappers.<DictType>lambdaQuery().in(DictType::getId, ids).eq(DictType::getState, YesOrNotEnum.Y.getCode())));
        }else {
            dictTypeDoCache(dictTypeService.getBaseMapper().selectList(Wrappers.<DictType>lambdaQuery().eq(DictType::getState, YesOrNotEnum.Y.getCode())));
        }
    }

    @Override
    public void dictCache(String dictTypeId, String dictIds) {
        DictType dictType = dictTypeService.getById(dictTypeId);
        if(ToolUtil.isNotEmpty(dictIds)){
            dictDoCache(dictType, dictService.list(Wrappers.<Dict>lambdaQuery().eq(Dict::getDictTypeId, dictTypeId).in(Dict::getId, dictIds).eq(Dict::getState, YesOrNotEnum.Y.getCode())));
        }else {
            dictDoCache(dictType, dictService.list(Wrappers.<Dict>lambdaQuery().eq(Dict::getDictTypeId, dictTypeId).eq(Dict::getState, YesOrNotEnum.Y.getCode())));
        }
    }

    /**
     * 字典 父
     * @param dictTypeList
     */
    public void dictTypeDoCache(List<DictType> dictTypeList){
        dictTypeList.stream().forEach(dictType -> {
            QueryWrapper wrapper = new QueryWrapper<>();
            wrapper.eq("dict_type_id", dictType.getId());
            wrapper.eq("state", YesOrNotEnum.Y.getCode());
            dictDoCache(dictType, dictService.getBaseMapper().selectList(wrapper));
        });
    }

    /**
     * 字典 子(实际数据)
     * @param dictType
     * @param dictList
     */
    public void dictDoCache(DictType dictType, List<Dict> dictList){
        if(dictType.getCode().equalsIgnoreCase(SystemCache.baseProperties.id)){   //系统管理基本配置
            ReflexUtils.configure(BaseProperties.class, SystemCache.baseProperties, dictList,
                    Arrays.asList(SystemCache.baseProperties.level_1,
                                  SystemCache.baseProperties.level_2,
                                  SystemCache.baseProperties.level_3));
            //全局共享变量
            BeetlConfiguration beetlConfiguration = beetlConfig.beetlConfiguration();
            Map<String, Object> shared = beetlConfiguration.getGroupTemplate().getSharedVars();
            shared.put("systemName", SystemCache.baseProperties.getSystemName());
            shared.put("welcomeTip", SystemCache.baseProperties.getWelcomeTip());
            beetlConfiguration.getGroupTemplate().setSharedVars(shared);
        }
    }

}
